package com.winway.sixbook.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class CounterDataManager {

	private static final String TAG = "Countermanager";
	private static final String DB_NAME = "sixbook";
	private static final String TABLE_NAME = "counttable";
	public static final String  ID = "_id";

	public static final String COUNTERNUM = "countnum";
	public static final String COUNTERNAME = "countname";
	public static final String ADDTIME = "add_time";
	public static final String COUNTTODAY = "counttoday";
	public static final String TARGETNUM = "targetnum";
	public static final String BEGINDATE = "begindate";
	public static final String ENDDATE = "enddate";
	
	private static final int DB_VERSION = 2;
	private Context mContext = null;
	private SQLiteDatabase mSQLiteDatabase = null;
	private DataBaseManagementHelper mDatabaseHelper = null;

	public CounterDataManager(Context context) {
		mContext = context;
		Log.i(TAG, "UserDataManager construction!");
	}

	public void openDataBase() throws SQLException {
		mDatabaseHelper = DataBaseManagementHelper.getInstance(mContext);
		mSQLiteDatabase = mDatabaseHelper.getWritableDatabase();
	}

	public void closeDataBase() throws SQLException {
		mDatabaseHelper.close();
	}

	public long insertCounterData(Integer countnum,String countname,Integer targetnum,String begindate,String enddate) {
		ContentValues values = new ContentValues();
		values.put(COUNTERNUM, countnum);
		values.put(COUNTERNAME, countname);
		values.put(TARGETNUM, targetnum);
		values.put(BEGINDATE, begindate);
		values.put(ENDDATE, enddate);
		return mSQLiteDatabase.insert(TABLE_NAME, ID, values);
	}

	public boolean updateTodayData(int id,int todaynum ,String datetime) {
		ContentValues values = new ContentValues();
		values.put(COUNTTODAY	,todaynum);
		values.put(ADDTIME	,datetime);
		return mSQLiteDatabase.update(TABLE_NAME, values, ID + "=" + id, null) > 0;
	}
	
	public boolean updateCounterData(int id,int counternum,int todaynum ) {
		ContentValues values = new ContentValues();
		values.put(COUNTERNUM	,counternum);
		values.put(COUNTTODAY	,todaynum);
		return mSQLiteDatabase.update(TABLE_NAME, values, ID + "=" + id, null) > 0;
	}
	
	public boolean updateCounterData2(int id,int counternum,String countername,Integer targetnum,String begindate,String enddate) {
		ContentValues values = new ContentValues();
		values.put(COUNTERNUM	,counternum);
		values.put(COUNTERNAME	,countername);
		values.put(TARGETNUM, targetnum);
		values.put(BEGINDATE, begindate);
		values.put(ENDDATE, enddate);
		return mSQLiteDatabase.update(TABLE_NAME, values, ID + "=" + id, null) > 0;
	}
	

	public Cursor fetchCounterData(int id) throws SQLException {
		Cursor mCursor = mSQLiteDatabase.query(false, TABLE_NAME, null, ID + "=" + id, null, null, null, null, null);

		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor fetchAllCounterDatas() {
//		return mSQLiteDatabase.query(TABLE_NAME, null, null, null, null, null, null);
		return mSQLiteDatabase.rawQuery("select * from "+TABLE_NAME, null);
	}

	public boolean deleteData(int id) {

		return mSQLiteDatabase.delete(TABLE_NAME, ID + "=" + id, null) > 0;
	}

	public boolean deleteAllDatas() {

		return mSQLiteDatabase.delete(TABLE_NAME, null, null) > 0;
	}
	
	public boolean updateCounterDataById(String columnName, int id, String columnValue) {
		ContentValues values = new ContentValues();
		values.put(columnName, columnValue);
		return mSQLiteDatabase.update(TABLE_NAME, values, ID + "=" + id, null) > 0;
	}
	
}
